package org.jeecg.modules.travel.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.travel.code.CheckStatusCode;
import org.jeecg.modules.travel.dto.slideshow.*;
import org.jeecg.modules.travel.entity.JhSlideshow;
import org.jeecg.modules.travel.service.IJhSlideshowService;
import org.jeecg.modules.travel.vo.slideshow.AppSlideshowRecordVo;
import org.jeecg.modules.travel.vo.slideshow.PcSlideshowDetailVo;
import org.jeecg.modules.travel.vo.slideshow.PcSlideshowRecordVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * @Create @author wentao.wang
 * @Create @date 2025-04-27 21:44:38
 */
@Tag(name="幻灯片放映表")
@RestController
@RequestMapping("/travel/jhSlideshow")
@Slf4j
public class JhSlideshowController extends JeecgController<JhSlideshow, IJhSlideshowService> {

    @Autowired
    private IJhSlideshowService jhSlideshowService;

    @Operation(summary="幻灯片放映-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<PcSlideshowRecordVo>> queryPageList(PcSlideshowPageDto dto, HttpServletRequest req) {
        JhSlideshow slideshow = new JhSlideshow();
        BeanUtils.copyProperties(dto, slideshow);
        QueryWrapper<JhSlideshow> queryWrapper = QueryGenerator.initQueryWrapper(slideshow, req.getParameterMap());
        queryWrapper.lambda().eq(JhSlideshow::getDelFlag, CommonConstant.DEL_FLAG_0);
        Page<JhSlideshow> page = new Page<>(dto.getPageNo(), dto.getPageSize());
        return Result.OK(jhSlideshowService.queryPage(page, queryWrapper));
    }

    /**
     *   添加
     *
     * @param dto
     * @return
     */
    @AutoLog(value = "幻灯片放映-添加")
    @Operation(summary="幻灯片放映-添加")
    @RequiresPermissions("org:jh_slideshow:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody @Valid SlideshowAddDto dto) {
        String res = jhSlideshowService.slideshowAdd(dto);
        if (StringUtils.isNotBlank(res)) {
            return Result.error(res);
        }
        return Result.OK("添加成功！");
    }

    /**
     *  编辑
     *
     * @param dto
     * @return
     */
    @AutoLog(value = "幻灯片放映-编辑")
    @Operation(summary="幻灯片放映-编辑")
    @RequiresPermissions("org:jh_slideshow:edit")
    @PostMapping(value = "/edit")
    public Result<String> edit(@RequestBody SlideshowEditDto dto) {
        String res = jhSlideshowService.slideshowEdit(dto);
        if (StringUtils.isNotBlank(res)) {
            return Result.error(res);
        }
        return Result.OK("编辑成功!");
    }

    /**
     *  审核
     *
     * @param dto
     * @return
     */
    @AutoLog(value = "幻灯片放映-审核")
    @Operation(summary="幻灯片放映-审核")
    @RequiresPermissions("org:jh_slideshow:auth")
    @PostMapping(value = "/auth")
    public Result<String> auth(@RequestBody @Valid SlideshowAuthDto dto) {
        String res = jhSlideshowService.slideshowAuth(dto,getUserId());
        if (StringUtils.isNotBlank(res)) {
            return Result.error(res);
        }
        return Result.OK("编辑成功!");
    }

    /**
     *   通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "幻灯片放映-通过id删除")
    @Operation(summary="幻灯片放映-通过id删除")
    @RequiresPermissions("org:jh_slideshow:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name="id",required=true) String id) {
        boolean flag = jhSlideshowService.removeById(id);
        if (!flag) {
            return Result.error("删除失败!");
        }
        return Result.OK("删除成功!");
    }

    /**
     *  批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "幻灯片放映-批量删除")
    @Operation(summary="幻灯片放映-批量删除")
    @RequiresPermissions("org:jh_slideshow:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        boolean flag = jhSlideshowService.removeByIds(Arrays.asList(ids.split(",")));
        if (!flag) {
            return Result.error("批量删除失败!");
        }
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @Operation(summary="幻灯片放映-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<PcSlideshowDetailVo> queryById(@RequestParam(name="id",required=true) String id) {
        JhSlideshow slideshow = jhSlideshowService.getById(id);
        if (slideshow == null) {
            return Result.error("幻灯片记录不存在");
        }
        PcSlideshowDetailVo detailVo = new PcSlideshowDetailVo();
        BeanUtils.copyProperties(slideshow, detailVo);
        return Result.OK(detailVo);
    }

    /* ======================================= 移动端 ======================================================= */

    @Operation(summary="幻灯片放映-分页列表查询")
    @GetMapping(value = "/app/appQueryList")
    public Result<IPage<AppSlideshowRecordVo>> appQueryList(AppSlideshowPageDto dto, HttpServletRequest req) {
        JhSlideshow slideshow = new JhSlideshow();
        QueryWrapper<JhSlideshow> queryWrapper = QueryGenerator.initQueryWrapper(slideshow, req.getParameterMap());
        queryWrapper.lambda().eq(JhSlideshow::getDelFlag, CommonConstant.DEL_FLAG_0)
                .eq(JhSlideshow::getCheckStatus, CheckStatusCode.APPROVED)
                .orderByAsc(JhSlideshow::getSortNo);
        Page<JhSlideshow> page = new Page<>(dto.getPageNo(), dto.getPageSize());
        return Result.OK(jhSlideshowService.appQueryList(page,queryWrapper));
    }

}
